chrome.extension.sendRequest({command: "options"}, appendToSmileys);

function appendToSmileys(response) {
    smileys = response.smileys;
    if(!(response.smileysEnabled && smileys)) {
        return;
    }
        
    var div = document.createElement('div');
    
    smileys = convertLineBreaks(smileys);
    var linesArray = smileys.split('\n');
    var chunksArray = new Array();
    for(var i = 0; i < linesArray.length; i++) {
        chunksArray = linesArray[i].split('|');
        div.appendChild(createSmiley(chunksArray[0], chunksArray[1]));    
    }
    
    document.getElementsByClassName('inner')[0].appendChild(div);
}

function createSmiley(smiley, url) {
    var link, image;
    link = document.createElement('a');
    link.setAttribute('href', '#');
    link.setAttribute('onclick', smileyOnClick(smiley));
    image = document.createElement('img');
    image.setAttribute('src', url);
    image.setAttribute('alt', smiley);
    image.setAttribute('title', smiley + ' (Kipin Pro)');
    link.appendChild(image);
    return link;
}

function smileyOnClick(smiley) {
    return "insert_text('" + smiley + "', true, true); return false;";
}